import request from "@/utils/request"
import type { CommunityItem, CommentItem } from "@/types/web/community"

/** 获取社区列表 */
export function getCommunityListAPI(page: number, pageSize: number, params?: {
  keyword?: string
  category?: string
  sort?: string
}) {
  return request<{
    data: CommunityItem[]
    total: number
    page: number
    pageSize: number
    totalPages: number
  }>({
    url: "/web/community/list",
    method: "get",
    params: {
      page,
      pageSize,
      ...params
    }
  })
}

/** 获取社区详情 */
export function getCommunityDetailAPI(id: string) {
  return request<{
    data: CommunityItem
  }>({
    url: `/web/community/detail/${id}`,
    method: "get"
  })
}

/** 获取社区评论列表 */
export function getCommunityCommentsAPI(id: string, page: number, pageSize: number) {
  return request<{
    data: CommentItem[]
    total: number
  }>({
    url: `/web/community/${id}/comments`,
    method: "get",
    params: {
      page,
      pageSize
    }
  })
}


/** 点赞社区帖子 */
export function likeCommunityAPI(id: string) {
  return request({
    url: `/web/community/${id}/like`,
    method: "post"
  })
}


/** 获取热门社区帖子 */
export function getHotCommunityAPI(limit: number = 10) {
  return request<{
    data: CommunityItem[]
  }>({
    url: "/web/community/hot",
    method: "get",
    params: {
      limit
    }
  })
}

/** 获取推荐社区帖子 */
export function getRecommendCommunityAPI(limit: number = 10) {
  return request<{
    data: CommunityItem[]
  }>({
    url: "/web/community/recommend",
    method: "get",
    params: {
      limit
    }
  })
}

/** 搜索社区帖子 */
export function searchCommunityAPI(keyword: string, page: number, pageSize: number, params?: {
  categoryID?: string
  sort?: string
}) {
  return request<{
    data: CommunityItem[]
    total: number
    page: number
    pageSize: number
    totalPages: number
  }>({
    url: "/web/community/search",
    method: "get",
    params: {
      keyword,
      page,
      pageSize,
      ...params
    }
  })
}

/** 获取分类列表 */
export function getCommunityCategoriesAPI() {
  return request<{
    data: Array<{
      _id: string
      name: string
      slug: string
      description?: string
      icon?: string
      color?: string
      postCount: number
    }>
  }>({
    url: "/web/community/categories",
    method: "get"
  })
}

/** 获取标签列表 */
export function getCommunityTagsAPI() {
  return request<{
    data: Array<{
      _id: string
      name: string
      slug: string
      color?: string
      postCount: number
    }>
  }>({
    url: "/web/community/tags",
    method: "get"
  })
}

/** 获取统计数据 */
export function getCommunityStatisticsAPI() {
  return request<{
    data: {
      totalPosts: number
      totalUsers: number
      totalViews: number
      todayPosts: number
      todayUsers: number
    }
  }>({
    url: "/web/community/statistics",
    method: "get"
  })
}

/** 发布帖子 */
export function publishCommunityAPI(data: {
  title: string
  description: string
  content: string
  cover?: string
  categoryID: string
  tags?: string[]
  isOriginal: boolean
  status: number
}) {
  return request<{
    data: {
      _id: string
      title: string
      status: number
      publishTime: string
    }
  }>({
    url: "/web/community/publish",
    method: "post",
    data
  })
}

/** 收藏帖子 */
export function favoriteCommunityAPI(id: string) {
  return request<{
    data: {
      isFavorited: boolean
      favorites: number
    }
  }>({
    url: `/web/community/${id}/favorite`,
    method: "post"
  })
}

/** 获取我的帖子 */
export function getMyPostsAPI(page: number, pageSize: number) {
  return request<{
    data: CommunityItem[]
    total: number
  }>({
    url: "/web/community/my/posts",
    method: "get",
    params: {
      page,
      pageSize
    }
  })
}

/** 获取我的收藏 */
export function getMyFavoritesAPI(page: number, pageSize: number) {
  return request<{
    data: CommunityItem[]
    total: number
  }>({
    url: "/web/community/my/favorites",
    method: "get",
    params: {
      page,
      pageSize
    }
  })
}

/** 获取浏览历史 */
export function getMyHistoryAPI(page: number, pageSize: number) {
  return request<{
    data: CommunityItem[]
    total: number
  }>({
    url: "/web/community/my/history",
    method: "get",
    params: {
      page,
      pageSize
    }
  })
}

